﻿Ext.define('app.prm.project.Summary', {
    extend: 'Ext.panel.Panel',
    border: false,
    scrollable: 'y',
    tbar: [
        "->",
        {
            text: '历史概要',
            glyph: 0xf1da,
            handler: function () {
                var history = Ext.create("app.prm.project.SummaryHistory");
                history.show();
            }
        }
    ],
    bodyStyle: 'padding:5px',
    initComponent: function () {
        var me = this;
        me.items = [
            me.getForm(),
            {
                title: '进行中项目',
                border: false,
                height: 200,
                layout: 'border',
                items: [
                me.getToDoGrid(),
                me.getEditorForm()
                ]
            },
            me.getNextGrid(),
            me.getTextArea(),
            me.getButton()
        ];
        me.callParent();
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                border: true,
                title: '',
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    margin: 6
                },
                items: [
                    {
                        xtype: 'displayfield',
                        fieldLabel: '研究室成员',
                        name: 'txt_labTeam',
                        value: ''
                    },
                    { xtype: 'hidden', name: 'labUserNum' },
                    { xtype: 'hidden', name: 'labUserNames' },
                    { xtype: 'hidden', name: 'laboratoryId' },
                    { xtype: 'hidden', name: 'year' },
                    { xtype: 'hidden', name: 'week' },
                    { xtype: 'hidden', name: 'startTime' },
                    { xtype: 'hidden', name: 'endTime' },
                    {
                        xtype: 'combo',
                        fieldLabel: '负载情况',
                        name: 'status',
                        store: ['未知', '满足', '缺项目', '缺人']
                    },
                    {
                        xtype: 'container',
                        flex: 1,
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'nowStaffNeed',
                                fieldLabel: '当前(空闲资源)'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'twoWeekNeed',
                                fieldLabel: '2周后'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'fourWeekNeed',
                                fieldLabel: '4周后'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'sixWeekNeed',
                                fieldLabel: '6周后'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'eightWeekNeed',
                                fieldLabel: '8周后'
                            }
                        ]
                    },
                    {
                        xtype: 'container',
                        flex: 1,
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'nowIdleNum',
                                fieldLabel: '当前(需要项目)'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'twoWeekIdleNum',
                                fieldLabel: '2周后'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'fourWeekIdleNum',
                                fieldLabel: '4周后'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'sixWeekIdleNum',
                                fieldLabel: '6周后'
                            },
                            {
                                xtype: 'textfield',
                                flex: 1,
                                name: 'eightWeekIdleNum',
                                fieldLabel: '8周后'
                            }
                        ]
                    }
                ]
            });
        }
        return this.form;
    },
    getToDoGrid: function () {
        if (!this.toDoGrid) {
            var main = this;
            this.toDoGrid = new Ext.grid.Panel({

                border: true,
                region: 'center',
                //viewConfig: {
                //    enableTextSelection: true //可以复制单元格文字
                //},
                autoHeight: true,
                forceFit: false,
                columns: [
                    { header: '项目名称', dataIndex: 'projectName', maxWidth: 200 },
                    { header: '项目编号', dataIndex: 'projectNo', maxWidth: 200 },
                    {
                        header: '项目成员', dataIndex: 'team'
                    },
                    {
                        header: '开始日期', maxWidth: 100, dataIndex: 'startDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        }
                    },
                    {
                        header: '应完成日期', maxWidth: 100, dataIndex: 'targetDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        }
                    },
                    {
                        header: '预计完成日期', maxWidth: 120, dataIndex: 'planDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        }
                    },
                    {
                        header: '备注', dataIndex: 'remark',
                        //renderer: function (value, p, record) {
                        //    p.tdStyle = 'white-space:normal;word-wrap: break-word;';
                        //    if (value == null)
                        //        value = 'N/A';
                        //    else
                        //    {
                        //        p.tdAttr = value;
                        //    }
                        //    return value;
                        //}
                    }
                ],
                store: new Ext.data.Store({
                    fields: [
                        'remark',
                        { name: 'id' },
                        'projectName',
                     { name: 'projectNo' },
                     { name: 'team' },
                     { name: 'startDate' },
                     { name: 'targetDate' },
                     { name: 'planDate' }
                    ],
                    data: []
                }),
                listeners: {
                    'selectionchange': function (se, records) {
                        main.activeRecord = records[0];
                        if (main.activeRecord) {
                            main.getEditorForm().setTitle(main.activeRecord.get('projectNo') + " 备注");
                            main.getEditorForm().loadRecord(main.activeRecord);
                        } else {
                            main.getEditorForm().reset();
                        }
                    }
                }
            });

            // this.toDoGrid.getSelectionModel().on('selectionchange', this.onSelectChange, this);

        }
        return this.toDoGrid;
    },
    getEditorForm: function () {
        var me = this;
        if (!me.editorFormRemark) {
            me.editorFormRemark = Ext.create("Ext.form.Panel", {
                region: 'east',
                width: 300,
                title: '备注',
                layout: 'fit',
                items: [
                    {
                        name: 'remark',
                        xtype: 'textarea'
                    }
                ],
                buttons: [
                    {
                        'text': '更新',
                        handler: function () {
                            me.editorFormRemark.updateRecord(me.activeRecord);
                        }
                    }
                ]
            });

        }
        return me.editorFormRemark;
    },
    getNextGrid: function () {
        var me = this;
        if (!this.nextGrid) {
            this.nextGrid = new Ext.grid.Panel({
                title: '下一步工作',
                border: true,
                style: 'margin:10px 0 0 0',
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                tbar: [
                    { text: '新增一行', icon: '/Content/images/add.png', handler: function () { me.addRow(); } }
                ],
                autoHeight: true,
                forceFit: false,
                plugins: {
                    ptype: 'cellediting',
                    clicksToEdit: 1
                },
                columns: [
                    { header: '项目名称', dataIndex: 'projectName', editor: {} },
                    { header: '项目编号', dataIndex: 'projectNo', editor: {} },
                    { header: '项目成员', dataIndex: 'team', editor: {} },
                    {
                        header: '开始日期', dataIndex: 'startDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        },
                        editor: { xtype: 'datefield' }
                    },
                    {
                        header: '应完成日期', dataIndex: 'targetDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        }, editor: { xtype: 'datefield' }
                    },
                    {
                        header: '预计完成日期', dataIndex: 'planDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        }, editor: { xtype: 'datefield' }
                    },
                    {
                        header: '实际完成日期', dataIndex: 'actualDate', renderer: function (value, p, record) {
                            var v = Ext.util.Format.date(value, 'Y-m-d')
                            return v;
                        }, editor: { xtype: 'datefield' }
                    },
                    { header: '备注', dataIndex: 'remark', editor: {} }
                ],
                store: new Ext.data.Store({
                    fields: [
                     'remark',
                     'projectName',
                     { name: 'projectNo' },
                     { name: 'team' },
                     { name: 'startDate' },
                     { name: 'targetDate' },
                     { name: 'planDate' },
                     { name: 'actualDate' }
                    ],
                    data: []
                })
            });
        }
        return this.nextGrid;
    },
    addRow: function () {
        var me = this;
        me.getNextGrid().store.add({});
    },
    getTextArea: function () {
        if (!this.textArea) {
            this.textArea = new Ext.form.field.TextArea({
                emptyText: '补充内容',
                height: 150,
                style: 'margin:10px 0 0 0',
                width: '100%'
            });
        }
        return this.textArea;
    },
    getButton: function () {
        var me = this;
        if (!this.summaryButton) {
            this.summaryButton = new Ext.Panel({
                border: false,
                buttonAlign: 'center',//居中
                buttons: [{ text: '确定', handler: function () { me.saveSummary(); } }]//按钮
            });
        }
        return this.summaryButton;
    },
    listeners: {
        afterrender: function (me) {
            //上海研发中心第一研究室 2016年32周 (2016-8-8 ~ 2016-8-14)
            App.AjaxGet('/api/services/app/summary/GetToEdit', function (data) {
                var record = data.result;
                console.log(record);

                var st = record.startTime.substring(0, 10);
                var et = record.endTime.substring(0, 10);
                me.getForm().setTitle(record.rdCenter + record.laboratory + "&nbsp;" + record.year + "年" + record.week + "周" + "&nbsp;(" + st + "&nbsp;~&nbsp;" + et + ")");
                var teams = record.labUserNum;
                if (record.labUserNum > 0) {
                    teams = record.labUserNum + "&nbsp;(" + record.labUserNames + ")";
                }
                me.form.getForm().setValues({ txt_labTeam: teams });
                me.getForm().getForm().setValues(record);
                me.getTextArea().setValue(record.note);
                me.getToDoGrid().store.loadData(record.toDos);
                if (record.nexts) {
                    me.getNextGrid().store.loadData(record.nexts);
                }
            });
        }
    },
    saveSummary: function () {
        var me = this;
        var forms = me.getForm().getForm().getValues();
        forms.note = me.getTextArea().getValue();
        var todoStore = me.getToDoGrid().store;
        var todos = [];
        for (var i = 0; i < todoStore.getCount() ; i++) {
            var record = todoStore.getAt(i);
            todos.push(record.data);
        }
        var nextStore = me.getNextGrid().store;
        var nexts = [];
        for (var i = 0; i < nextStore.getCount() ; i++) {
            var record = nextStore.getAt(i);
            nexts.push(record.data);
        }
        forms.ToDoProject = JSON.stringify(todos);
        forms.NextProject = JSON.stringify(nexts);
        App.AjaxPost('/api/services/app/summary/CreateOrEdit', forms, function (data) {
            Ext.Msg.alert('系统提示', "保存成功");
        });
    }
})